package org.kealinghornets.nxtdroid;

import org.kealinghornets.nxtdroid.NXT.LightSensor;
import org.kealinghornets.nxtdroid.NXT.NXTThread;
import org.kealinghornets.nxtdroid.NXT.TouchSensor;

/**
 * Created by jychuah on 11/3/13.
 */
public class MyNXTThread extends NXTThread {

    int state = 1;
    LightSensor eyeball;

    public MyNXTThread() {
        setNXTThreadName("Bridge Crosser");
    }

    public void run() {
        while (state != -1) {
            switch (state) {
                case 1:
                    do_state_1();
                    break;
                case 2:
                    do_state_2();
                    break;
                case 3:
                    do_state_3();
                    break;
                case 4:
                    do_state_4();
                    break;
                case 5:
                    do_state_5();
                    break;
                case 6:
                    do_state_6();
                    break;
                default:
                    state = -1;
                    break;
            }
        }
    }

    public void do_state_1() {
        eyeball = getActiveLightSensor(PORT_1);
        log("I'm in state 1");
        runMotor(PORT_A, -100);
        runMotor(PORT_B);
        wait(2300);
        if (eyeball.getLightValue() > 500) {
            state = 2;
        } else {
                state = 3;


        }
    }

    public void do_state_2() {
        // do some stuff
        log("I'm in state 2");
        runMotor(PORT_A, -100);runMotor(PORT_B);
        wait(4600);
        runMotor(PORT_A);runMotor(PORT_B);
        wait(7500);
        runMotor(PORT_A, -100);runMotor(PORT_B);
        wait(2300);
        runMotor(PORT_A);runMotor(PORT_B);
        wait(18000);
        runMotor(PORT_A, -100);runMotor(PORT_B);
        wait(2300);
        runMotor(PORT_A);runMotor(PORT_B);
        wait(7500);
        if (eyeball.getLightValue() > 500) {
            state = 4;
        } else {
                state = 3;

        }
    }

    public void do_state_3() {
        // do some stuff
        log("I'm in state 3");
        runMotor(PORT_A);runMotor(PORT_B);
        wait(7500);
        runMotor(PORT_A, -100);runMotor(PORT_B);
        wait(2300);
        runMotor(PORT_A);runMotor(PORT_B);
        wait(7500);
        runMotor(PORT_A);runMotor(PORT_B, -100);
        wait(2300);
        runMotor(PORT_A);runMotor(PORT_B);
        wait(7500);
        if (eyeball.getLightValue() > 500) {
            state = 4;
        } else {
                state = 5;
            }

        }
    }

    public void do_state_4() {
        log("I'm in state 4");
        runMotor(PORT_A);runMotor(PORT_B);
        wait(6000);
        runMotor(PORT_A);runMotor(PORT_B, -100);
        wait(2300);
        runMotor(PORT_A);runMotor(PORT_B);
        wait(7500);
        if (eyeball.getLightValue() > 500) {
            state = 6;
        } else {
                state = 7;
            }
        reset();
        state = 5;

        }
    }

    public void do_state_5() {
        log("I'm in state 5");
        runMotor(PORT_A, -100);runMotor(PORT_B);
        wait(4600);
        runMotor(PORT_A);runMotor(PORT_B);
        wait(18000);
        runMotor(PORT_A, -100);runMotor(PORT_B);
        wait(2300);
        runMotor(PORT_A);runMotor(PORT_B);
        wait(2500);
        if (eyeball.getLightValue() > 500) {
            state = 6;
        } else {
            if (eyeball.getLightValue() < 500) {
                state = 7;
            } else {
        }
        reset();
        state = 6;

        }
    }
    public void do_state_6() {
        log("I'm in state 6");
        runMotor(PORT_C);
        wait(3000);
        reset();
    }
    public void do_state_7() {
        log("I'm in state 7");
        runMotor(PORT_A, -100);runMotor(PORT_B);
        wait(4600);
        runMotor(PORT_A);runMotor(PORT_B);
        wait(18000);
        if (eyeball.getLightValue() > 500) {
            state = 6;
        } else {
            reset();
            state = 6;
        }
    }
}